import grok
from persistent.dict import PersistentDict

class FinancialHistory(grok.Application, grok.Container):
    def __init__(self, initialBalance=0):
        super(FinancialHistory, self).__init__()
        self.cashOnHand = initialBalance
        self.incomes = PersistentDict()      # 
        self.expenditures = PersistentDict() #

    def receiveFrom(self, source, amount):
        self.incomes[source] = self.totalReceivedFrom(source) + amount
        self.cashOnHand += amount
        
    def spendFor(self, reason, amount):
        self.expenditures[reason] = self.totalSpentFor(reason) + amount
        self.cashOnHand -= amount
        
    def totalReceivedFrom(self, source):
        return self.incomes.get(source, 0)
    
    def totalSpentFor(self, reason):
        return self.expenditures.get(reason, 0)
        

class Index(grok.View):
    pass # see app_templates/index.pt